package com.ellricky.teacher.ui.mycourse.course

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.ClassRecord
import com.ellricky.baselib.entity.new.ClassRecordData
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.baselib.util.StringUtils
import com.ellricky.teacher.MyCourseAdapter
import com.ellricky.teacher.R
import kotlinx.android.synthetic.main.tc_fragment_class_record.*
import java.util.*
import kotlin.collections.ArrayList

open class ClassRecordFragment : BaseFragment<Void, BasePresenter<Void>>(), RvRefreshListener,
    AdapterView.OnItemClickListener {

    private val adapter by lazy {
        MyCourseAdapter.ClassRecordAdapter(requireActivity())
    }

    var refresh: Boolean = false
    var paramMap: MutableMap<String, String>? = mutableMapOf()
    var dataSource: ArrayList<ClassRecordData> = arrayListOf()
    var time: Long = 0

    companion object {
        fun newInstance(): ClassRecordFragment {
            val fragment = ClassRecordFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.tc_fragment_class_record
    }

    override fun initUIView(p0: View?) {
        rvView.init(this)
        loadDataOnInit()

        time = System.currentTimeMillis()
        val timeString = StringUtils.conversionTime(time, "MM")
            .replace("0", "").plus("月")
        tv_month.text = timeString

        icLeftArrow.setOnClickListener {
            time = getPreMonth()
            tv_month.text = StringUtils.conversionTime(time, "MM")
                .replace("0", "").plus("月")
            loadDataOnInit()
        }

        icRightArrow.setOnClickListener {
            time = getNextMonth()
            tv_month.text = StringUtils.conversionTime(time, "MM")
                .replace("0", "").plus("月")
            loadDataOnInit()
        }
    }

    private fun getPreMonth(): Long {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        calendar.add(Calendar.MONTH, -1)
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        return calendar.timeInMillis
    }

    private fun getNextMonth(): Long {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        calendar.add(Calendar.MONTH, +1)
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        return calendar.timeInMillis
    }

    override fun setAdapter(rv: RecyclerView) {
        adapter.setOnItemClickListener(this)
        rv.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        this.refresh = refresh
        loadCourseTimeData()
    }

    private fun loadCourseTimeData() {
        val url = EAppUtil.getApiHost()
        initParamMap()
        val timeString = StringUtils.conversionTime(time, "yyyy-MM")
        paramMap?.apply {
            put("action", "teacherCourseNumList")
            put("eid", LocalCacheUtil.getUid())
            put("school_id", LocalCacheUtil.getSchoolId())
            put("time", timeString)
            put("page_size", "40")
        }

        HttpUtils.getInstance().postFrom(url, paramMap,
            object : NewEntityCallback<ClassRecord>(mContext) {
                override fun onSuccess(data: ClassRecord, msg: String) {
                    tvClassCount.text = data.totalNum.toString()
                    tv_class_all_num.text = "本月总计：${data.monthNum}课时"
                    this@ClassRecordFragment.dataSource = data.list
                    rvView.onLoadFinish(data.list, refresh)
                }
            })
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    private fun initParamMap() {
        paramMap?.put("page", rvView.curPage.toString())
    }

    override fun updateAdapterData(p0: Boolean) {
        if (refresh) adapter.refresh(dataSource) else adapter.loadMore(dataSource)
    }
}